package com.yxhl.km.module.utils.album;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class PhotoUtil {
    public static final int CAMERA = 1;// 动态调用照相机
    public static final int PICK_PHOTO = 2;// 动态调用相册
    public static final int SHOW = 3;// 全屏显示照片
    public static final int EDIT = 4;// 编辑图片
    public static final int ADD = 55; //添加图片
    public static final int DELETE = 55; //添加图片
    public static final int CANCLE_IMAGE = 76; //取消操作

    public static String getPath(Intent data, Activity act) {
        Uri uri;
        if (data == null)
            return null;
        uri = data.getData();
        if (uri == null)
            return null;
        // 如果是从百度云等使用file协议获取到的图片(由于google photo在国内不稳定,如果从google
        // photo加载的图片可能会无法获取)
        if (uri.getScheme().equals("file"))
            return uri.getPath();
        // 解决sdk4.4下无法获取到路径的问题
        if (!CompatibleHeigherSDKUtil.isHeigherThanKITKAT()) {
            String[] pojo = {MediaStore.Images.Media.DATA};
            Cursor cursor = act.managedQuery(uri, pojo, null, null, null);
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }
        } else
            return CompatibleHeigherSDKUtil.getPatch_SDK4_4(act, uri);
        return null;
    }


    public static Bitmap cacheImage(File file) {
        Bitmap photo = null;
        try {
            ExifInterface exifInterface = new ExifInterface(file.getPath());
            int result = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED);
            int rotate = 0;
            switch (result) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                default:
                    break;
            }
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file.getPath(), opts);
            opts.inSampleSize = computeSampleSize(opts, -1, 720 * 1280);
            opts.inJustDecodeBounds = false;
            photo = BitmapFactory.decodeFile(file.getPath(), opts);
            if (rotate > 0) {
                Matrix matrix = new Matrix();
                matrix.setRotate(rotate);
                Bitmap rotateBitmap = Bitmap.createBitmap(photo, 0, 0,
                        opts.outWidth, opts.outHeight, matrix, true);
                if (rotateBitmap != null) {
                    photo.recycle();
                    photo = rotateBitmap;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return photo;
    }

    public static void saveBitmap(File file, Bitmap bm) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    }

    public static int computeSampleSize(BitmapFactory.Options options,
                                        int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,
                                                int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}
